﻿<?php
// +------------------------------------------------------------------------------	
//	@desc Bootstart And Load For the Website
//  @date Wed Aug 19 CST 2009
//  @author Fly Jin <flysharping@gamil.com>
// +------------------------------------------------------------------------------

	// +加载核心文件------------------------------------------------------
	require_once(PATH_SPRING.'Function.php');
	require_once(PATH_SPRING.'Init.php');
	
	//获取$control And $action-------------------------------------------
	$param  =$_SERVER['REQUEST_URI'];
	if (empty($param) || $param == ""){
		die("Can't fetch SERVER['SCRIPT_NAME'],Please config it manually in 'index.php'!");
	}
	$param  =substr($param,1);
	if (strrpos($param,'.')) {
		$param  =substr($param,0,strrpos($param,'.'));
	}
	$param  =explode('/', $param);
	$control=!empty($param[0]) ? $param[0] : 'index';		//控制器
	$action =isset($param[1]) ? $param[1] : 'index';		//方法名称
	
	//重组$_GET参数------------------------------------------------------
	$sumParam = count($param);
	if ($sumParam>2) {
		for ($i=2;$i<$sumParam;$i++) {
			$param[$param[$i]] = $param[++$i];
		}
	}

	//去除无用参数-------------------------------------------------------
	foreach ($param as $k=>$v){
		if (is_int($k)) {
			unset($param[$k]);
		}
		if(empty($v)){
			unset($param[$k]);
		}
	}
	$_GET=$param;
	unset($param);
	
	//Execute ---------------------------------------------------------
	$controlClass = $control.'Controller';
	if (file_exists(PATH_CONTROLLER.$controlClass.'.php')) {
		require_once(PATH_CONTROLLER.$controlClass.'.php');
		$controller = new $controlClass();
		$controller->meta();
		if (method_exists($controller, $action)) {
			$model	= new Model();
			$db		= $model->connect_db();
			$model	= loadModel($control);
			if ($model!==false) {
				$model->db = $db;
				$controller->$control = $model;
			}
			unset($model);
			$controller->$action();
		}else{
			$controller->index();
		}
		$assign	= $controller->assign;
		//获取模板文件名
		if (isset($controller->display) && $controller->display) {
			$displayFile = PATH_VIEW.$controller->display; 
		}else{
			$displayFile = PATH_VIEW.$control.DS.$action.'.htm';
		}
	}else{ 
		redirect404();
	}
?>